package com.mcc.app.group.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcc.app.group.domain.GroupTeamUser;
import com.mcc.app.group.mapper.GroupTeamUserMapper;
import com.mcc.app.group.service.IGroupTeamUserService;
import com.mcc.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 班组人员Service业务层处理
 *
 * @author gxy
 * @date 2022-03-22
 */
@Service
public class GroupTeamUserServiceImpl implements IGroupTeamUserService {

    @Autowired
    private GroupTeamUserMapper groupTeamUserMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询班组人员
     *
     * @param id 班组人员ID
     * @return 班组人员
     */
    @Override
    public GroupTeamUser selectGroupTeamUserById(Long id) {
        return groupTeamUserMapper.selectById(id);
    }

    /**
     * 查询班组人员列表
     *
     * @param groupTeamUser 班组人员
     * @return 班组人员
     */
    @Override
    public List<GroupTeamUser> selectGroupTeamUserList(GroupTeamUser groupTeamUser) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(groupTeamUser)) {
            queryWrapper.eq(ObjectUtil.isNotEmpty(groupTeamUser), "team_id", groupTeamUser.getTeamId());
        }
        List<GroupTeamUser> res = groupTeamUserMapper.selectList(queryWrapper);
        for (int i = 0; i < res.size(); i++) {
            GroupTeamUser row = res.get(i);
            row.setUser(sysUserService.selectUserById(row.getUserId()));
        }
        return res;
    }

    /**
     * 新增班组人员
     *
     * @param groupTeamUser 班组人员
     * @return 结果
     */
    @Override
    public int insertGroupTeamUser(GroupTeamUser groupTeamUser) {
        return groupTeamUserMapper.insert(groupTeamUser);
    }

    /**
     * 修改班组人员
     *
     * @param groupTeamUser 班组人员
     * @return 结果
     */
    @Override
    public int updateGroupTeamUser(GroupTeamUser groupTeamUser) {
        return groupTeamUserMapper.updateById(groupTeamUser);
    }

    /**
     * 删除班组人员
     *
     * @param id 班组人员ID
     * @return 结果
     */
    @Override
    public int deleteGroupTeamUserById(Long id) {
        return groupTeamUserMapper.deleteById(id);
    }

    /**
     * 批量删除班组人员
     *
     * @param ids 班组人员ID
     * @return 结果
     */
    @Override
    public int deleteGroupTeamUserByIds(Long[] ids) {
        if (ids.length == 0) return 0;
        return groupTeamUserMapper.deleteBatchIds(ListUtil.toList(ids));
    }

    @Override
    public boolean deleteGroupTeamUserByTeamId(Long teamId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("team_id", teamId);
        return groupTeamUserMapper.delete(queryWrapper) > 0;
    }
}
